import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from utils import read_config
# 读取配置信息
config = read_config()
sender_email = config.get('mail','sender_email')
sender_password = config.get('mail','sender_password')
receiver_email = config.get('mail','receiver_email')

def send_email(subject, body,receiver_email=receiver_email,attachment_path=None):
    # # 设置发件人和收件人邮箱地址
    # sender_email = "your_email@sina.com"  # 你的Sina邮箱或Outlook邮箱
    # receiver_email = receiver_email

    # 创建一个MIMEMultipart对象
    message = MIMEMultipart()
    message["From"] = sender_email
    message["To"] = receiver_email
    message["Subject"] = subject

    # 添加邮件正文
    message.attach(MIMEText(body, "plain"))

    # 添加附件（如果有）
    if attachment_path:
        with open(attachment_path, "rb") as attachment:
            part = MIMEApplication(attachment.read(), Name="data.csv")
        part['Content-Disposition'] = f'attachment; filename="{attachment_path}"'
        message.attach(part)

    # 连接SMTP服务器
    with smtplib.SMTP("smtp.sina.cn", port=25) as server:
        server.login(sender_email, sender_password)  # 输入Sina邮箱或Outlook邮箱的密码
        # 发送邮件
        server.sendmail(sender_email, receiver_email, message.as_string())

